package com.alcj.controller;

import com.alcj.common.constant.SysConstant;
import com.alcj.common.utils.CmnUtil;
import com.alcj.model.Address;
import com.alcj.model.Order;
import com.alcj.model.User;
import com.alcj.service.AddressService;
import com.alcj.service.OrderDetailListService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by jdz on 2017/11/7.
 */

@Controller
@RequestMapping("/orderDetailList")
public class OrderDetailListController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    OrderDetailListService orderDetailListService;


    /**
     * @param request
     * @param model
     * @return String
     * @Author: guofsh
     * @Description:初期表示信息
     */
    @RequestMapping("/queryOrderDetail")
    public String queryOrdeer(HttpServletRequest request, Model model) {
        try {
            // 获取登录用户
            User sysUser =  new User();
            sysUser.setUserid("cf37a7e346b44fae96d6262e1b719828");
//            User sysUser = (User) request.getSession().getAttribute("User");
            // 获取登录用户判断用户Id跟用户类型是否为空
            if (CmnUtil.isEmpty(sysUser)) {

                return SysConstant.INDEX_PAGE;

            } else {
                //取得用户Id
                String sysUserId = sysUser.getUserid();
                // 获取当前页数
                String currentPage = request.getParameter("currentPage");
                // 设置页数
                if (CmnUtil.isEmpty(currentPage)) {
                    currentPage = "1";
                }
                // 查询用户订单信息
                Order order = new Order();
                order.setUserid(sysUserId);
                List<Order> list = orderDetailListService.selectListByUserIdAndStatusNew(order);
                model.addAttribute("Status","StatusFist");
                //把用户订单信息存入model
                model.addAttribute("listOrder", list);
            }

        } catch (Exception e) {
            // 异常报错
            logger.error("获取用户订单信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :", e);
            return SysConstant.ERROR_PAGE_500;
        }
        return "/orderDetailList";
    }

    /**
     * @param request
     * @param model
     * @return String
     * @Author: guofsh
     * @Description:初期表示信息
     */
    @RequestMapping("/queryOrderDetailOld")
    public String queryOrdeerOld(HttpServletRequest request, Model model) {
        try {
            // 获取登录用户
            User sysUser =  new User();
            sysUser.setUserid("cf37a7e346b44fae96d6262e1b719828");
//            User sysUser = (User) request.getSession().getAttribute("User");
            // 获取登录用户判断用户Id跟用户类型是否为空
            if (CmnUtil.isEmpty(sysUser)) {

                return SysConstant.INDEX_PAGE;

            } else {
                //取得用户Id
                String sysUserId = sysUser.getUserid();
                // 获取当前页数
                String currentPage = request.getParameter("currentPage");
                // 设置页数
                if (CmnUtil.isEmpty(currentPage)) {
                    currentPage = "1";
                }
                // 查询用户订单信息
                Order order = new Order();
                order.setUserid(sysUserId);
                List<Order> list = orderDetailListService.selectListByUserIdAndStatusOld(order);
                model.addAttribute("Status","StatusLast");
                //把用户订单信息存入model
                model.addAttribute("listOrder", list);
            }

        } catch (Exception e) {
            // 异常报错
            logger.error("获取用户订单信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :", e);
            return SysConstant.ERROR_PAGE_500;
        }
        return "/orderDetailList";
    }
    /**
     * @param request
     * @param model
     * @return String
     * @Author: guofsh
     * @Description:删除订单信息
     */
    @RequestMapping("/deleteOrderDetail")
    public String deleteOrdeer(HttpServletRequest request, Model model) {
        try {
            // 获取登录用户
            User sysUser =  new User();
            sysUser.setUserid("11");
//            User sysUser = (User) request.getSession().getAttribute("User");
            // 获取登录用户判断用户Id跟用户类型是否为空
            if (CmnUtil.isEmpty(sysUser)) {

                return SysConstant.INDEX_PAGE;

            } else {
                //取得用户Id
                String sysUserId = sysUser.getUserid();
                String orderId =   request.getParameter("orderId");
                int status = orderDetailListService.deleteByPrimaryKey(orderId);
            }

        } catch (Exception e) {
            // 异常报错
            logger.error("删除用户订单信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :", e);
            return SysConstant.ERROR_PAGE_500;
        }
        return "redirect:/orderDetailList/queryOrderDetail";
    }
}

